/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fresher.servletcontroller;

import com.vng.jcore.profiler.ProfilerLog;
import fresher.model.ModelManager;
import fresher.model.TableManageModelSingleton;
import fresher.objects.Table;
import fresher.profilerLogUtility.WriteProfilerLog;
import fresher.utility.TemplateManager;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import hapax.TemplateException;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author quangtm5
 */
public class TableManagementServletController extends HttpServlet{

    private static final String URL_TABLE_INDEX_PAGE = "/table/table-manage.html";
    private static final String URL_ADD_NEW_TABLE = "/table/add-new-table.html";
    private static final String URL_SET_FREE_TABLE = "/table/set-free-table.html";
    private static final String URL_SET_FULL_TABLE = "/table/set-full-table.html";
    private static final String URL_DELETE_TABLE = "/table/delete-table.html";
    private static final String URL_GET_LOG = "/table/get-log.html";
    
    WriteProfilerLog writeProfilerLog = WriteProfilerLog.getWriteProfilerLog();
    

    private ProfilerLog profiler;
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doProcess(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doProcess(req, resp);
    }
    
    public void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        System.out.println(request.getRequestURI());
        String str_RequestURL = request.getRequestURI();
        
        profiler = new ProfilerLog();
        
        profiler.doStartLog("doProcess");
        
        switch(str_RequestURL)
        {
            case URL_TABLE_INDEX_PAGE:
            {
                //show index page of table management
                profiler.doStartLog("index");
                index(request, response);
                profiler.doEndLog("index");
                break;
            }
                
            case URL_DELETE_TABLE:
            {
                //delete a table
                profiler.doStartLog("delete");
                deleteTable(request, response);
                profiler.doEndLog("delete");
                break;
            }
                
            case URL_ADD_NEW_TABLE:
            {
                //insert a new table
                profiler.doStartLog("insert");
                insertTable(request, response);
                profiler.doEndLog("insert");
                break;
            }
                
            case URL_SET_FREE_TABLE:
            {
                //set table's status FREE
                profiler.doStartLog("setFree");
                setFreeTale(request, response);
                profiler.doEndLog("setFree");
                break;
            }
                
            case URL_SET_FULL_TABLE:
            {
                //set table's status FULL
                profiler.doStartLog("setFull");
                setFullTable(request, response);
                profiler.doEndLog("setFull");
                break;
            }
                
            case URL_GET_LOG:
            {
                writeProfilerLog.saveToFile();
                break;
            }
           
            default:
            {
                Logger.getLogger(TableManagementServletController.class.getName()).log(Level.SEVERE,null, "NOT EXIST MATCHES REQUEST URL");
            }
        }
        profiler.doEndLog("doProcess");
        
//        System.out.print(profiler.dumpLogConsole());
        writeProfilerLog.writeLogToFile(profiler);
        
        System.out.print(profiler.dumpLogConsole());
        
        
    }
    
    
    public void index(HttpServletRequest request, HttpServletResponse response)
                    throws IOException
    {
//        profiler.doStartLog("indexCreateTableManageModel");
        TableManageModelSingleton model = ModelManager.getTableModel();
//        profiler.doEndLog("indexCreateTableManageModel");
        
//        profiler.doStartLog("indexGetListOfTable");
        ArrayList<Table> listOfTable = model.getListTable();
//            System.out.println(listOfTable.size());
//        profiler.doEndLog("indexGetListOfTable");

        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

       

       

        TemplateDataDictionary dictionary = TemplateDictionary.create();

        for(int i = 0; i < listOfTable.size(); i++)
        {
            TemplateDataDictionary table = dictionary.addSection("TABLE");
            table.setVariable("table.id", String.valueOf(listOfTable.get(i).getId()));

            //if status == 0 => Free table
            if(listOfTable.get(i).getStatus() == 0)
            {
                table.setVariable("table.status", "FREE");
            }
            //Full table
            else
            {
                table.setVariable("table.status", "FULL");
            }


        }

       //Render output 
        String content = "";
        try {
            content = TemplateManager.getInstance().getTemplate("/view/table.xtm").renderToString(dictionary);
        } catch (TemplateException ex) {
            ErrorServletController.showError(request, response, ex.getMessage());
        }
       
        response.getWriter().println(content);
    }
    
    public void deleteTable(HttpServletRequest request, HttpServletResponse response)
                    throws IOException
    {
        response.setStatus(HttpServletResponse.SC_OK);
            
        TableManageModelSingleton model = ModelManager.getTableModel();

        int id = Integer.parseInt(request.getParameter("id"));

        int i_check = model.deleteTable(id);

        response.sendRedirect("/table/table-manage.html");
    }
    
    public void insertTable(HttpServletRequest request, HttpServletResponse response)
                    throws IOException
    {
        response.setStatus(HttpServletResponse.SC_OK);
            
        
        TableManageModelSingleton model = ModelManager.getTableModel();

        //insert a new table
        int i_check = model.insertTable();

        response.sendRedirect("/table/table-manage.html");
    }
    
    public void setFreeTale(HttpServletRequest request, HttpServletResponse response)
                    throws IOException
    {
        response.setStatus(HttpServletResponse.SC_OK);
            
        TableManageModelSingleton model = ModelManager.getTableModel();

        int id = Integer.parseInt(request.getParameter("id"));

        int i_check = model.setFreeTable(id);

        response.sendRedirect("/table/table-manage.html");
    }
    
    public void setFullTable(HttpServletRequest request, HttpServletResponse response)
                    throws IOException
    {
        response.setStatus(HttpServletResponse.SC_OK);
            
        TableManageModelSingleton model = ModelManager.getTableModel();

        int id = Integer.parseInt(request.getParameter("id"));

        int i_check = model.setFullTable(id);

        response.sendRedirect("/table/table-manage.html");
    }
}
